import { Stages } from "mmb";
import { getEnvStage } from "../../store/helper";
import { getUser } from "../../service/userService";
import { upsertUserFan } from "../../service/userFanService";
import { upsertUserFollow } from "../../service/userFollowService";
import Router from "koa-router";
const router = new Router();

router.post("/_cmd/robot/addUserFan", async (ctx, next) => {
  if (getEnvStage() === Stages.PROD) {
    ctx.throw(403, "forbid user robot api")
  }
  const {userId, userIdAsFan} = ctx.request.body;
  const user = await getUser(userIdAsFan)
  const { nickName, mobile, openId} = user
  const aliasName = nickName || mobile || openId
  const now = Date.now()
  const userFan = await upsertUserFan({userId: user.userId, followerUserId: userIdAsFan, beFollowed: now, aliasName})
  const userFollow = await upsertUserFollow({userId: userIdAsFan, followedUserId: user.userId, followed: now})
  ctx.body = {userFan, userFollow};
});

router.post("/_cmd/robot/addUserFollow", async (ctx, next) => {
  if (getEnvStage() === Stages.PROD) {
    ctx.throw(403, "forbid user robot api")
  }
  const {userId, userIdAsFollow} = ctx.request.body;
  const user = await getUser(userId)
  const { nickName, mobile, openId} = user
  const aliasName = nickName || mobile || openId
  const now = Date.now()
  const userFollow = await upsertUserFollow({userId: user.userId, followedUserId: userIdAsFollow, followed: now})
  const userFan = await upsertUserFan({userId: userIdAsFollow, followerUserId: user.userId, beFollowed: now, aliasName})
  ctx.body = {userFollow, userFan};
});

export default router;
